/**
 * 结算方式管理
 */
Ext.namespace('Divo');
Ext.namespace('Divo.app');

Divo.app.WorkerList = function() {
  /* ----------------------- private属性 ----------------------- */
  var canEdit = false, status = "list"; // 当前列表状态("list","insert","edit")
  var gridEl = 'Worker-grid';
  var gridAdd;
  var grid, grid2, form1, form2, selModel, dModel, toolbar1, toolbar2, win1, win2;
  var data, data2, sId;
  var resourceId = "128";
  /* ----------------------- private方法 ----------------------- */
  // 创建列表
  function createGrid() {
    var queryFlds = new Array();
    var searchFlds = new Ext.util.MixedCollection();
    queryFlds.push('name');
    var fldName = new Ext.form.TextField({
          name : 'name',
          id : gridEl + '-name',
          fieldLabel : '名称',
          allowBlank : true,
          width : 100

        });
    searchFlds.add('name', fldName);

    grid = new Divo.Base.GridView({
          remoteSort : true,
          gridId : gridEl,
          queryFlds : queryFlds,
          searchFlds : searchFlds,
          entityClassName : 'com.our.sys.project1.entity.Worker',
          queryUrl : 'worker/query',
          deleteUrl : 'worker/delete',
          jsonId : 'id',
          recordPk : ["id"]
        });
    selModel = grid.getSelectionModel();
    selModel.on('rowselect', onRowSelect);

    grid2 = new Divo.Base.GridView({
          hasBbar : true,
          remoteSort : true,
          gridId : queryFlds,
          searchFlds : searchFlds,
          entityClassName : 'com.our.sys.project1.entity.DMaterials',
          queryUrl : 'smaterials/query_d',
          deleteUrl : 'smaterials/delete_d',
          jsonId : 'id',
          recordPk : ["id"]
        });

    dModel = grid2.getSelectionModel();
    dModel.on('rowselect', onDRowSelect);

    grid2.getStore().on('beforeload', function(store, options) {
          store.baseParams.sId = sId ? sId : '';
        }, this, true);

    var cm = grid.getColumnModel();
    for (i = 0; i < cm.getColumnCount(); i++) {
      if (cm.getDataIndex(i) == "state") {
        cm.setRenderer(i, function(val, params, record) {
              if (val == "1") {
                return "无效";
              }

              if (val == "2") {
                return "有效";
              }
            });
      }
    }

    var cm2 = grid2.getColumnModel();
    for (i = 0; i < cm2.getColumnCount(); i++) {
      if (cm2.getDataIndex(i) == "state") {
        cm2.setRenderer(i, function(val, params, record) {
              if (val == "1") {
                return "无效";
              }

              if (val == "2") {
                return "有效";
              }
            });
      }
    }
  }

  function onDRowSelect(selModel, rowIndex, record) {
    data2 = record.data;
  }
  // 选择某行
  function onRowSelect(selModel, rowIndex, record) {
    data = record.data;
    sId = data.id;
    canEdit = 'N';
    status = "list";
    // setToolbarStatus();
    grid2.getStore().reload();
  }

  // 刷新列表
  function reloadRecords() {
    grid.getStore().reload();
    status = "list";
    // setToolbarStatus();
  }
  function reloadRecord2() {
    grid2.getStore().reload();
  }
  // 创建新的记录，初始化表单
  function createNewRecord() {

    form.loadRecord(new form.dataRecordMeta({
          _p_record_status : "insert",
          id : "",
          code : "",
          name : ""
        }));
    win = new Ext.Window({
          title : '新增工作人员 ',
          iconCls : 'icon-win',
          autoHeight : true,
          width : 320,
          layout : 'form',
          plain : false,
          buttonAlign : 'right',
          closable : false,
          modal : true,
          items : [form]
        });
    var yesBtn = win.addButton({
          text : '确定'
        }, commitForm, this);
    var cancelBtn = win.addButton({
          text : '关闭'
        }, onWinHide, this);

    win.show();
  }
  function onWinHide() {
    win.hide();
  }
  // 创建新的记录，初始化表单
  function createRecords() {

    form2.loadRecord(new form.dataRecordMeta({
          _p_record_status : "insert",
          id : "",
          name : "",
          sId : sId
        }));
    win2 = new Ext.Window({
          title : '新增二级级科目信息',
          iconCls : 'icon-win',
          autoHeight : true,
          width : 360,
          layout : 'form',
          plain : false,
          buttonAlign : 'right',
          closable : false,
          modal : true,
          items : [form2]
        });
    var yesBtn = win2.addButton({
          text : '确定'
        }, commitbankForm, this);
    var cancelBtn = win2.addButton({
          text : '关闭'
        }, onbankwinHide, this);

    win2.show();
  }

  function onbankwinHide() {
    win2.hide();
  }

  // 切换记录编辑状态
  function EditMode2() {
    form2.loadRecord(new form.dataRecordMeta({
          _p_record_status : "insert",
          id : data2.id,
          name : data2.name,
          sId : sId
        }));
    win2 = new Ext.Window({
          title : '编辑二级科目信息',
          iconCls : 'icon-win',
          autoHeight : true,
          width : 360,
          layout : 'form',
          plain : false,
          buttonAlign : 'right',
          closable : false,
          modal : true,
          items : [form2]
        });
    var yesBtn = win2.addButton({
          text : '确定'
        }, commitbankForm, this);
    var cancelBtn = win2.addButton({
          text : '关闭'
        }, onbankwinHide, this);

    win2.show();
  }
  // 创建form表单
  function createForm() {
    form = new Divo.app.WorkerForm({
          layout : 'fit',
          region : 'east',
          border : true,
          split : true,
          width : 320,
          minSize : 300,
          maxSize : 400,
          submitUrl : 'worker/save'
        });

    form.on('commitFormSuccess', afterCommitFormSuccess);

    /*
     * form2 = new Divo.app.DMaterialsForm({ layout : 'fit', region : 'east',
     * border : true, split : true, width : 350, minSize : 300, maxSize : 400,
     * submitUrl : 'smaterials/save_d' });
     * 
     * form2.on('commitFormSuccess', afterCommitFormSuccess2);
     */
  }

  // 新建或修改表单成功提交后,重新加载其父节点
  function afterCommitFormSuccess(operation, recordData) {
    grid.getStore().reload();
  }

  function afterCommitFormSuccess2(operation, recordData) {
    grid2.getStore().reload();
  }

  // 创建layout
  function createLayout() {
    toolbar = new Ext.Toolbar({
          items : [{
                xtype : "button",
                cls : "x-btn-icon",
                icon : "resources/img/g_rec_src.png",
                tooltip : "过滤数据",
                id : "tlb_FILTER",
                handler : toggleFilterBar
              }, {
                icon : "resources/img/g_rec_new.png",
                text : "新增",
                id : "tlb_NEW",
                handler : createNewRecord
              }, {
                icon : "resources/img/g_rec_del.png",
                text : "删除",
                id : "tlb_DELETE",
                handler : deleteRecord
              }, '-', {
                icon : "resources/img/g_rec_upd.png",
                text : "编辑",
                id : "tlb_EDIT",
                handler : toggleEditMode
              }, {
                icon : "resources/img/g_rec_refresh.gif",
                text : "刷新",
                id : "tlb_REFRESH",
                handler : reloadRecords
              }]
        });
    toolbar2 = new Ext.Toolbar({
          items : [{
                icon : "resources/img/g_rec_new.png",
                text : "新增",
                id : "tlb_NEWSS",
                handler : createRecords
              }, {
                icon : "resources/img/g_rec_del.png",
                text : "删除",
                id : "tlb_DELETESS",
                handler : deleteRecordbank
              }, '-', {
                icon : "resources/img/g_rec_upd.png",
                text : "编辑",
                id : "tlb_EDITSS",
                handler : EditMode2
              }, {
                icon : "resources/img/g_rec_refresh.gif",
                text : "刷新",
                id : "tlb_REFRESHSS",
                handler : reloadRecord2
              }]
        });
    var viewport = new Ext.Viewport({
          layout : 'border',
          items : [{
                layout : 'border',
                region : 'center',
                border : false,
                items : [{
                      layout : 'fit',
                      region : 'north',
                      border : false,
                      items : [toolbar],
                      height : 25
                    }, {
                      layout : 'fit',
                      region : 'center',
                      border : false,
                      items : [grid]
                    }]
              }]
        });

    viewport.doLayout();
  }

  // 删除记录
  function deleteRecord() {
    grid.deleteRecord();
  }
  // 删除记录
  function deleteRecordbank() {
    grid2.deleteRecord();
  }
  // 切换过滤查询工具条状态
  function toggleFilterBar() {
    grid.toggleFilterBar();
  }

  // 切换记录编辑状态
  function toggleEditMode() {
    form.loadRecord(new form.dataRecordMeta({
          _p_record_status : "insert",
          id : data.id,
          code : data.code,
          name : data.name
        }));
    win = new Ext.Window({
          title : '编辑工作人员 ',
          iconCls : 'icon-win',
          autoHeight : true,
          width : 320,
          layout : 'form',
          plain : false,
          buttonAlign : 'right',
          closable : false,
          modal : true,
          items : [form]
        });
    var yesBtn = win.addButton({
          text : '确定'
        }, commitForm, this);
    var cancelBtn = win.addButton({
          text : '关闭'
        }, onWinHide, this);

    win.show();
  }

  // 提交表单，保存新建或修改的信息
  function commitForm() {
    form.commitForm();
    win.hide();
  }
  function commitbankForm() {
    form2.commitForm();
    win2.hide();
  }
  // 加载数据完成后
  function onLoad(store, records, options) {
    status = "list";
    // setToolbarStatus();
  }

  // 设置工具条状态
  function setToolbarStatus() {
    var checkMethod = Divo.util.Tools.checkMethod(resourceId);
    var checkList = checkMethod.split(",");
    var thisMethodList = [];
    thisMethodList.push("tlb_NEW");
    thisMethodList.push("tlb_DELETE");
    thisMethodList.push("tlb_EDIT");
    var noMethod = [];
    for (var j = 0; j < thisMethodList.length; j++) {
      var thisMethod = thisMethodList[j];
      var flag = false;
      for (var i = 0; i < checkList.length; i++) {
        var method = checkList[i];
        if (method == thisMethod) {
          flag = true;
        }
      }
      if (!flag) {
        noMethod.push(thisMethod);
      }
    }

    for (var i = 0; i < noMethod.length; i++) {
      var id = noMethod[i];
      toolbar.items.get(id).disable();
    }
  }

  /* ----------------------- public方法 ----------------------- */
  return {
    /**
     * 初始化
     */
    init : function() {
      createGrid();
      createForm();
      createLayout();
      grid.load();
      grid.getStore().on("load", onLoad);
      setToolbarStatus();
    }
  }
}();

Ext.onReady(Divo.app.WorkerList.init, Divo.app.WorkerList, true);